package com.d2rv.guide;

import com.d2rv.model.InstructionModel;
import com.d2rv.offscreen.OffScreen3D;

/**
 * This class extends Instruction and draws arrows instruction used by the guide
 * for the collaboration.
 *
 * @author Darrieutort A., Dupouy A., Rossignol L., Van Poucke A.
 */
public class InstructionArrow extends Instruction {

    private boolean[] arrow;
    private float angle;

    /**
     * Default constructor.
     *
     * @param instructionmodel reference to a model to draw
     */
    public InstructionArrow(InstructionModel instructionmodel) {
        super(instructionmodel);
        this.arrow = new boolean[3];
        this.angle = 0;

        for (int i = 0; i < 3; i++) {
            arrow[i] = false;
        }
    }

    /**
     * Finds the arrow to draw.
     *
     * @return the arrow to draw, -1 if nothing to draw
     */
    private int arrowToDraw() {
        for (int i = 0; i < 3; i++) {
            if (arrow[i]) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Defines the arrow to draw by setting the current arrow to false, then
     * setting the i to true.
     *
     * @param i the arrow to draw
     */
    public void instructionToDraw(int i) {
        int j = arrowToDraw();
        if (j != -1) {
            this.arrow[j] = false;
        }
        this.arrow[i] = true;
    }

    /**
     * If something to draw, rotates the current arrow correctly.
     */
    private void angle() {
        int toDraw = this.arrowToDraw();
        if (toDraw != -1) {
            if (toDraw == 0) {
                this.angle = 90;
            } else if (toDraw == 1) {
                this.angle = -90;
            } else if (toDraw == 2) {
                this.angle = 0;
            }
        }
    }

    /**
     * Draws the current arrow instruction model.
     *
     * @param parent the current offscreen
     */
    public void draw(OffScreen3D parent) {
        angle();
        parent.pushMatrix();
        parent.translate(this.instructionmodel.position().x, this.instructionmodel.position().y, this.instructionmodel.position().z);
        parent.rotateX((float) Math.toRadians(this.angle));
        this.instructionmodel.draw(parent);
        parent.popMatrix();
    }
}